package com.jorado.search.core.util;

import com.jorado.core.utility.ConvertUtils;
import org.apache.solr.common.SolrDocument;

import static com.jorado.core.utility.ConvertUtils.isDataNull;

public final class SolrUtils {

    private SolrUtils() {

    }

    public static String getDocValue(SolrDocument doc, String key) {

        return getDocValue(doc, key, "");

    }

    public static <T> T getDocValue(SolrDocument doc, String key, T defaultValue) {

        try {

            if (!doc.containsKey(key)) {
                return defaultValue;
            }

            Object o = doc.get(key);

            if (isDataNull(o)) {
                return defaultValue;
            }

            return ConvertUtils.cast(o, defaultValue);

        } catch (Exception ex) {

            return defaultValue;
        }

    }
}
